package com.nissoft.framework.web;

import java.io.IOException;
import java.nio.charset.Charset;

import org.springframework.http.HttpInputMessage;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.MediaType;
import org.springframework.http.converter.AbstractHttpMessageConverter;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;

import com.nissoft.framework.util.JsonUtil;

public class GsonHttpMessageConverter extends
		AbstractHttpMessageConverter<Object> {

	private JsonUtil gt = new JsonUtil();
	public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");

	public GsonHttpMessageConverter() {
		super(new MediaType("application", "json"));
	}

	@Override
	protected boolean supports(Class<?> clazz) {

		return true;
	}

	@Override
	protected Object readInternal(Class<? extends Object> clazz,
			HttpInputMessage inputMessage) throws IOException,
			HttpMessageNotReadableException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected void writeInternal(Object t, HttpOutputMessage outputMessage)
			throws IOException, HttpMessageNotWritableException {

		String json = gt.toJson(t);
		outputMessage.getBody().write(json.getBytes());
	}
}
